import * as adminService from './service';

export default {
  namespace: 'user_manage',
  state: {
    data: [],
    total:null,
    page:null,
    pageSize:null,
    roleList: null
  },
  effects: {
    //查询
    *queryUser({payload:{currentPage}},{call,put}){
      const {list,total,page,pageSize} = yield call(adminService.query,{currentPage});
      //console.log('list'+JSON.stringify(list))
      yield put({
        type: 'stateHandler',
        payload:{
          list,
          total: parseInt(total,10),
          page: parseInt(page,10),
          pageSize: parseInt(pageSize,10)
        }
      })
    },
    //添加,更新
    *saveUser({payload:{user}},{call,put,select}){
      yield call(adminService.save,{user});
      const page = yield select((state) => state.user_manage.page);
      yield put({
        type: 'queryUser',
        payload: {currentPage: page}
      })
    },
    //删除
    *deleteUser({payload: {id}},{call,put,select}){
      yield call(adminService.del,{id});
      const page = yield select((state) => state.user_manage.page);
      yield put({
        type: 'queryUser',
        payload: { currentPage:page }
      })
    },
    //获得角色列表
    *getRoleList(_,{call,put}){
      const roles = yield call(adminService.getRoleList);
      //console.log('9999'+roles)
      yield put({
        type:'queryRoles',
        payload: {roles:roles}
      })
    }
  },
  reducers: {
    //查询用户列表分页
    stateHandler(state,{payload:{list:data,total,page,pageSize}}){
      return {...state,data,total,page,pageSize};
    },
    //查询角色列表
    queryRoles(state,  {payload: { roles: roleList}}){
     // console.log('9998' + roleList)
      return { ...state, roleList}
    }
  }
}
